<?php
namespace app\channel\controller\sys_admin;
use app\AdminController;
use think\Db;
use app\channel\model\ReportLogModel;
use app\channel\model\ProxyLevelModel;
use app\channel\model\ProxyUsersModel;
/**
 * 举报管理
 * Class Index
 * @package app\store\controller
 */
class Report extends AdminController
{
	/*------------------------------------------------------ */
	//-- 优先执行
	/*------------------------------------------------------ */
	public function initialize(){
        parent::initialize();
        $this->Model = new ReportLogModel();
    }
	/*------------------------------------------------------ */
	//-- 主页
	/*------------------------------------------------------ */
    public function index(){
        $this->assign("start_date", date('Y/m/01', strtotime("-1 year")));
        $this->assign("end_date", date('Y/m/d'));
        $this->getList(true);
		return $this->fetch();
	}
    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false) {

        $search['report_type'] = input('report_type',0,'intval');
        $search['status'] = input('status',0,'intval');
        $search['proxy_id'] = input('proxy_id',0,'intval');
        $search['keyword'] = input('keyword','','trim');
        $reportrange = input('reportrange');
        $where = [];
        if ($search['report_type'] > 0){
            $where[] = ['report_type','=',$search['report_type']];
        }
        if (empty($reportrange) == false){
            $dtime = explode('-',$reportrange);
            $where[] = ['add_time','between',[strtotime($dtime[0]),strtotime($dtime[1])+86399]];
        }else{
            $where[] = ['add_time','between',[strtotime("-1 year"),time()]];
        }
        if ($search['status'] >= 0){
            $where[] = ['status','=',$search['status']];
        }
        if (empty($search['keyword']) == false){
            $UsersModel = new UsersModel();
            $uids = $UsersModel->where(" mobile LIKE '%".$search['keyword']."%' OR user_id = '".$search['keyword']."'")->column('user_id');
            $uids[] = -1;//增加这个为了以上查询为空时，限制本次主查询失效
            $where[] = ['user_id','in',$uids];
        }
        $data = $this->getPageList($this->Model,$where);
        $ProxyUsersModel = new ProxyUsersModel();
        foreach ($data['list'] as $key=>$row){
            if ($row['new_supply_uid'] == 0 && $row['check_time'] == 0){
                $row['new_supply_uid'] = $ProxyUsersModel->getSupplyUid($row['user_id'],$row['up_proxy_id'],$ProxyLevelList);
            }
            $data['list'][$key] = $row;
        }
        $this->assign("reportType",$this->Model->reportType);
        $this->assign("search", $search);
        $this->assign("status", $this->Model->status);
        $this->assign("data", $data);
        if ($runData == false){
            $data['content']= $this->fetch('list')->getContent();
            unset($data['list']);
            return $this->success('','',$data);
        }
        return true;
    }
    /*------------------------------------------------------ */
    //-- 详情
    /*------------------------------------------------------ */
    public function info() {
        $log_id = input('log_id',0,'intval');
        if ($log_id < 1){
            return $this->error('传参错误.');
        }
        $row = $this->Model->where('log_id',$log_id)->find();
        if (empty($row)){
            return $this->error('没有找到相关记录.');
        }
        $row['imgs'] = explode(',',$row['imgs']);
        $this->assign('row',$row->toArray());
        $this->assign('status',$this->Model->status);
        $this->assign("reportType",$this->Model->reportType);
        return $this->fetch('info');
    }

    /*------------------------------------------------------ */
    //-- 审核
    /*------------------------------------------------------ */
    public function check() {
        $log_id = input('log_id',0,'intval');
        if ($log_id < 1){
            return $this->error('传参错误.');
        }
        $row = $this->Model->where('log_id',$log_id)->find();
        if (empty($row)){
            return $this->error('没有找到相关记录.');
        }
        $operating = input('operating','','trim');
        $remark = input('remark','','trim');
        $upData['check_time'] = time();
        $upData['check_auid'] = AUID;
        $upData['remark'] = $remark;
        if ($operating == 'refuse'){
            if (empty($admin_note) == true){
                return $this->error('请填写操作备注.');
            }
            $upData['status'] = 1;
        }else{
            $upData['status'] = 9;
        }
        $res = $this->Model->where('log_id',$log_id)->update($upData);
        if ($res < 1){
            return $this->error('更新审核结果失败.');
        }
        return $this->success('操作成功.');
    }

}
